package com.example.demo.controller;


import com.example.demo.exception.BusyException;
import com.example.demo.exception.file.InvalidNewpathException;
import com.example.demo.exception.file.InvalidPathException;
import com.example.demo.service.FileService;
import com.example.demo.utils.Message;
import com.jcraft.jsch.SftpException;
import com.lowagie.text.DocumentException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Map;


@CrossOrigin
@RestController
@RequestMapping("file")
public class FileController {

    @Resource
    FileService fileService;
    /**
     * 上传文件/图片
     * @param path 文件路径
     * @param file 文件
     * @return 消息
     */
    @ResponseBody
    @RequestMapping(value = "uploadFile", method = RequestMethod.POST)
    public Message uploadFile(@RequestParam("path") String path, @RequestParam("file") MultipartFile file) {
        String msg = "上传文件/图片成功";
        try{
            fileService.uploadFile(path,file);
            return Message.createSuc(msg);
        } catch (Exception e) {
            e.printStackTrace();
            msg="系统繁忙，上传文件/图片失败";
            return Message.createErr(msg);
        }
    }
    /**
     * 文件加星
     * @param path 文件完整路径
     * @return 是否加星成功
     */
    @RequestMapping(value="fileAddStar",method=RequestMethod.POST)
    public Message fileAddStar(@RequestParam("path") String path){
        String msg="文件加星成功";
        try {
            fileService.fileAddStar(path);
            return Message.createSuc(msg);
        } catch (InvalidPathException e) {
            msg="该文件路径不存在";
            return Message.createErr(msg);
        }

    }
    /**
     * 文件取消加星
     * @param path 文件完整路径
     * @return 是否取消成功
     */
    @RequestMapping(value="fileCancelStar",method = RequestMethod.POST)
    public Message fileCancelStar(@RequestParam("path") String path){
        String msg="文件取消加星成功";
        try {
            fileService.fileCancelStar(path);
            return Message.createSuc(msg);
        } catch (InvalidPathException e) {
            msg="该文件路径不存在";
            return Message.createErr(msg);
        }
    }
    /**
     * 文件置顶
     * @param path 文件完整路径
     * @return 是否置顶成功
     */
    @RequestMapping(value="fileAddTop",method = RequestMethod.POST)
    public Message fileAddTop(@RequestParam("path") String path){
        String msg="文件置顶成功";
        try {
            fileService.fileAddTop(path);
            return Message.createSuc(msg);
        } catch (InvalidPathException e) {
            msg="该文件路径不存在";
            return Message.createErr(msg);
        }
    }
    /**
     * 文件取消置顶
     * @param path 文件完整路径
     * @return 否则取消成功
     */
    @RequestMapping(value="fileCancelTop",method = RequestMethod.POST)
    public Message fileCancelTop(@RequestParam("path") String path){
        String msg="文件取消置顶成功";
        try {
            fileService.fileCancelTop(path);
            return Message.createSuc(msg);
        } catch (InvalidPathException e) {
            msg="该文件路径不存在";
            return Message.createErr(msg);
        }
    }
    /**
     *获取文件信息
     * @param path 文件完整路径
     * @return 文件信息
     */
    @RequestMapping(value="getFileInfo",method = RequestMethod.GET)
    public Message getFileInfo(@RequestParam("path") String path){
        String msg="文件路径有误，获取文件信息失败";
        try {
            Map<String,Object> myMap=fileService.getFileInfo(path);
            return Message.createSuc(myMap);
        } catch (InvalidPathException e) {
            return Message.createErr(msg);
        }

    }
    /**
     * 重命名文件夹或者文件
     * @param oldPath 原文件/夹名
     * @param newPath 新文件/夹名
     * @return 是否重命名成功
     */
    @RequestMapping(value="renameFile",method = RequestMethod.POST)
    public Message renameFile(@RequestParam("oldPath") String oldPath,@RequestParam("newPath") String newPath){
        String msg="重命名成功";
        try {
            fileService.renameFile(oldPath,newPath);
            return Message.createSuc(msg);
        } catch (InvalidPathException e) {
            msg="原文件路径有误，重命名失败";
            return Message.createErr(msg);
        }catch (SftpException e){
            msg="系统繁忙，重命名失败";
            return Message.createErr(msg);
        }
    }
    /**
     * 粘贴文件/夹
     * @param oldPath 原路径
     * @param newPath 新路径
     * @return 是否粘贴成功
     */
    @RequestMapping(value="pasteFile",method = RequestMethod.POST)
    public Message pasteFile(@RequestParam("oldPath") String oldPath,@RequestParam("newPath") String newPath){
        String msg="粘贴成功";
        try {
            fileService.pasteFile(oldPath,newPath);
            return Message.createSuc(msg);
        } catch (InvalidPathException e) {
            e.printStackTrace();
            msg="原文件路径有误，粘贴失败";
            return Message.createErr(msg);
        } catch (InvalidNewpathException e) {
            e.printStackTrace();
            msg="目标路径不符合规范，粘贴失败";
            return Message.createErr(msg);
        }
    }
     /**
     * 保存笔记
     * @param path 笔记的完整路径
     * @param title 笔记的标题
     * @param file 笔记文件
     * @param pics 非必填参数，图片文件
     * @return 是否保存成功
     */
    @ResponseBody
    @RequestMapping(value = "saveNote",method = RequestMethod.POST)
    public Message saveFileWithText(@RequestParam("path") String path, @RequestParam("title") String title, @RequestParam("file") MultipartFile file, @RequestParam(value = "pics", required = false) ArrayList<MultipartFile> pics,@RequestParam(value="summary") String summary){
        String msg="保存笔记成功";
        try{
            fileService.saveFileWithWord(path,title,file,pics,summary);
            return Message.createSuc(msg);
        } catch (Exception e) {
            e.printStackTrace();
            msg="系统繁忙，保存笔记失败";
            return Message.createErr(msg);
        }

    }

    /**
     * 获取文件内容
     * @param path 文件路径
     * @return 文件内容
     */
    @RequestMapping(value="getFilePath",method = RequestMethod.GET)
    public Message getFilePath(@RequestParam("path") String path){
        String msg="文件路径有误，获取文件内容失败";
        try{
            msg=fileService.getFilePath(path);
            return Message.createSuc(msg);
        } catch (InvalidPathException e) {
            e.printStackTrace();
            return Message.createErr(msg);
        }

    }

    /**
     * 下载文件
     * @param path 文件路径
     * @return 下载文件的地址
     */
    @RequestMapping(value="downloadFile",method = RequestMethod.GET)
    public Message downloadFile(@RequestParam("path") String path){
        String msg="原路径有误，下载文件失败";
        try{
            String allPath=fileService.getFilePath(path);
            return Message.createSuc(allPath);
        } catch (InvalidPathException e) {
            e.printStackTrace();
            return Message.createErr(msg);
        }
    }


    /**
     *导入word文件
     * @param path 文件的路径
     * @param file word文件
     * @return 是否导入成功
     */
    @RequestMapping(value="importWord",method = RequestMethod.POST)
    public Message importWord(@RequestParam("path") String path,@RequestParam("file") MultipartFile file){
        String msg="导入文件成功";
        try{
            fileService.importWord(path,file);
            return Message.createSuc(msg);
        } catch (Exception e) {
            e.printStackTrace();
            msg="系统繁忙，导入失败";
            return Message.createErr(msg);
        }

    }

    /**
     * 导出word文件
     * @param path 文件路径
     * @return 生成的word文件路径
     */
    @RequestMapping(value="exportWord",method = RequestMethod.GET)
    public Message exportWord(@RequestParam("path") String path){
        String msg="文件路径有误，导出失败";
        try {
            String filePath=fileService.exportWord(path);
            return Message.createSuc(filePath);
        } catch (SftpException | InvalidPathException e) {
            e.printStackTrace();
           return Message.createErr(msg);
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
            msg="系统繁忙，导出失败";
            return Message.createErr(msg);
        }
    }

    /**
     * md文件转pdf
     * @param path md文件路径
     * @return pdf路径
     */
    @RequestMapping(value="md2pdf",method = RequestMethod.GET)
    public Message md2pdf(@RequestParam("path") String path){
        String msg="文件路径有误，转出失败";
        try {
            String pdfPath=fileService.md2pdf(path);
            return Message.createSuc(pdfPath);
        } catch (FileNotFoundException | SftpException | InvalidPathException e) {
            e.printStackTrace();
            return Message.createErr(msg);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            msg="文件内容存在不支持的编码，转出失败";
            return Message.createErr(msg);
        }
    }
    @RequestMapping(value = "editFile",method = RequestMethod.POST)
    public Message editFile(@RequestParam("path")String path,@RequestParam(value = "author",required = false)String author,@RequestParam(value = "origin",required = false)String origin){
        String msg="文件编辑成功";
        try{
            fileService.editFile(path,author,origin);
            return Message.createSuc(msg);
        } catch (InvalidPathException e) {
            msg="文件路径错误，无法编辑";
            e.printStackTrace();
            return Message.createErr(msg);
        }catch(BusyException e){
            msg="系统繁忙，请稍等";
            return Message.createErr(msg);
        }
    }

}
